import { defineStore } from 'pinia';
import { ref } from 'vue'
import router from '@/router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { settings } from 'nprogress';
// const accessFn = (key, value) => {
//   key = key.charAt(0).toUpperCase() + key.slice(1)
//   return {
//     [`get${key}`]: function () { return value.__v_isRef ? value.value : value },
//     [`set${key}`]: function (newV) { value.__v_isRef ? value.value = newV : value = newV },
//     [`reset${key}`]: function () { value.__v_isRef ? value.value = '' : value = '' },
//   }
// }
export const useUsersStore = defineStore('user', {
  state: () => {
    return {
      access_token: ref(''),
      username: ref(''),
      avatar: ref(''),
      login_operation: ref('login'),
      last_get_code_time: ref(''),
    }
  },
  getters: {},
  actions: {
    login(info) {
      // axios.get('xxxx',info)
      console.log('userInfo:', info);
      if (!info.username.includes('樊')) return Promise.reject({
        msg: '请输入正确的用户名'
      })
      let response = {
        payload: {
          username: info.username,
          avatar: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F58a657c5-b96b-47c7-b6f4-09281158f442%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1711693835&t=8fdd397156ac6148aff53eb96b69ae45',
          token: 'adhaifhosdifsdogisa1a21da2d1'
        }
      }
      this.access_token = response.payload.token
      this.username = response.payload.username
      this.avatar = response.payload.avatar
      router.replace('/')
      setTimeout(() => {
        ElMessage.success('登录成功!')
      }, 1000);
      return Promise.resolve()
    },
    loginByPhone(info) {
      // axios.get('xxxx',info)
      console.log('phoneInfo:', info);
      if (parseInt(info.code) % 2 === 0) {
        return Promise.reject({
          msg: '验证码错误!请重新输入'
        })
      }
      let response = {
        payload: {
          username: info.phoneNumber,
          avatar: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F58a657c5-b96b-47c7-b6f4-09281158f442%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1711693835&t=8fdd397156ac6148aff53eb96b69ae45',
          token: 'adhaifhosdifsdogisa1a21da2d1'
        }
      }
      this.access_token = response.payload.token
      this.username = response.payload.username
      this.avatar = response.payload.avatar
      router.replace('/')
      setTimeout(() => {
        ElMessage.success('登录成功!')
      }, 1000);
      return Promise.resolve()
    },
    register(info) {
      // axios.get('xxxx',info)
      console.log('userInfo:', info);
      if (!info.username.includes('樊')) return Promise.reject({
        msg: '请输入正确的用户名'
      })
      let response = {
        payload: {
          username: info.username,
          avatar: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F58a657c5-b96b-47c7-b6f4-09281158f442%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1711693835&t=8fdd397156ac6148aff53eb96b69ae45',
          token: 'adhaifhosdifsdogisa1a21da2d1'
        }
      }
      this.access_token = response.payload.token
      this.username = response.payload.username
      this.avatar = response.payload.avatar
      router.replace('/')
      setTimeout(() => {
        ElMessage.success('注册并登录成功!')
      }, 1000);
      return Promise.resolve()
    },
    logout() {
      ElMessageBox.alert('确认退出登录？', '提示', {
        confirmButtonText: '确认',
        callback: (action) => {
          if (action === 'confirm') {
            this.access_token = ''
            this.username = ''
            this.avatar = ''
            ElMessage.success('退出成功!')
            window.location.reload()
          }
        },
      })
    },
    set_login_operation(val) {
      this.login_operation = val
    },
    set_last_get_code_time(val) {
      this.last_get_code_time = val
    }
  },
  persist: true
});


